package cn.org.timespace.plat.socket.mina.util;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.ons.util.SystemContext;
import cn.org.timespace.plat.service.mc.PlatMcInfoService;
import cn.org.timespace.plat.service.mc.PlatMconfService;
import cn.org.timespace.plat.socket.mina.filter.ESPlatProtocalPack;
import cn.org.timespace.plat.vo.mc.PlatMcInfo;
import cn.org.timespace.plat.vo.mc.PlatMconf;

public class ProcessPlatPackage {
private final static Log log = LogFactory.getLog(ProcessPlatPackage.class);
	
	/**
	 * 获取配置信息
	 * @param recordId
	 */
	public static PlatMconf getPlatMconf(String  mcId) {
		log.debug("信息终端 ID：" + mcId);
		PlatMconfService platMconfService = (PlatMconfService)SystemContext.getApplicationContext().getBean("platMconfService");
		//查询配置信息
		PlatMconf platMconf = platMconfService.findByMcId(mcId);
		return platMconf;
	}
	
	public static PlatMcInfo getPlatMcInfo(String  mcId,String packageContents){
		PlatMcInfoService platMcInfoService = (PlatMcInfoService)SystemContext.getApplicationContext().getBean("platMcInfoService");
		
		PlatMcInfo platMcInfo=new PlatMcInfo();
		String gatewayIp = packageContents.substring(0, 8);
		String gatewayMask = packageContents.substring(0, 8);
		String gatewayAddr = packageContents.substring(0, 8);
		String gatewayPort = packageContents.substring(0, 8);
		String upgradeServerIp = packageContents.substring(0, 8);
		String upgradeServerPort = packageContents.substring(0, 8);
		String dts = packageContents.substring(0, 8);
		String momIp = packageContents.substring(0, 8);
		String momPort = packageContents.substring(0, 8);
		String baudRate = packageContents.substring(0, 8);
		String teleNumber = packageContents.substring(0, 8);
		String apn = packageContents.substring(0, 8);
		String wirelessUser = packageContents.substring(0, 8);
		String wirelessPws = packageContents.substring(0, 8);
		String gatewayNo = packageContents.substring(0, 8);
		String edtof = packageContents.substring(0, 8);
		String bannedRepeat = packageContents.substring(0, 8);
		String manHead = packageContents.substring(0, 8);
		String minorHead = packageContents.substring(0, 8);
		String dhcp = packageContents.substring(0, 8);
		String momDomain = packageContents.substring(0, 8);
		String upgradeServerDomain = packageContents.substring(0, 8);
		String macAddr = packageContents.substring(0, 8);
		
//		Date updateTime;
//		String status;
//		
//		String conf;
		
		
		platMcInfo.setGatewayIp(gatewayIp);
		platMcInfoService.save(platMcInfo);
		return null;
	}
	
	
	
	/**
	 * 根据接收包结构取得回复包（机器号和功能号相同）
	 * @param pack
	 * @return
	 */
	public static ESPlatProtocalPack getPackReplay(ESPlatProtocalPack pack) {
		ESPlatProtocalPack packReplay = new ESPlatProtocalPack();
		packReplay.setSyncFrame(ESPlatProtocalPack.SYNC_FRAME_NAME_DOWN);
		packReplay.setMachineNo(pack.getMachineNo());
		packReplay.setFunctionNo(pack.getFunctionNo());
		return packReplay;
	}
	
	/**
	 * 字符串长度不足时，在最前面补 0
	 * @param digits  字符串应有长度
	 * @param str  字符串
	 * */
	public static String StrAddLength(int digits,String str){
		int len = str.length();
		if(len < digits){
			StringBuffer sb = new StringBuffer();
			for(int j = 0; j < digits - len; j++){
				sb.append("0");
			}
			str = sb.toString() + str;
		}
		return str;
	}
	
	/**
	 * 判断输入流是否以同步帧(SyncFrame)开头
	 * @param in
	 * @return
	 */
	public static boolean isStartWithSyncFrame(String inStr) {
		
		if(inStr == null)
			return false;
		
		if(inStr.startsWith(ESPlatProtocalPack.SYNC_FRAME_NAME_UP) || inStr.startsWith(ESPlatProtocalPack.SYNC_FRAME_NAME_DOWN))
			return true;
		
		return false;
	}
	
	public static String getStringTime(String longTime){
		return longTime.trim().replace("-", "").replace(" ", "").replace(":", "");
	}
	
	
	
	public static void main(String[] args) {
		//测试用
//		for(int i = 0; i < 2000; i++) {
//			//ProcessPackage.sendMessage("0000818819/110528070013/1/100000;");
//		}
//		
//		try {
//		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
//		Date dateCardRecord = format.parse("110528070013");
//		
//		Date dateActiveMsg = format.parse("110528070514");
//		
//		System.out.println((dateActiveMsg.getTime() - dateCardRecord.getTime()) / 1000);
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		System.out.println(ProcessPlatPackage.getStringTime("2012-04-12 14:35:33"));
	}
}